package com.xmcowell.sportpacemaster.views;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.xmcowell.sportpacemaster.R;
import com.xmcowell.sportpacemaster.config.Contents;
import com.xmcowell.sportpacemaster.config.Type;
import com.xmcowell.sportpacemaster.layout.MainActivity;

import java.util.List;

/**
 * Created by Administrator on 2017/6/8 0008.
 */

public class MainRightPopupView extends PopupWindow implements View.OnClickListener {
    public List<String> mData;
    public Context mContext;
    public View mView;
    public int currentWidth;
    public int currentHeight;
    private int num;

    private int tvW = 160;//全部textview的宽度与总宽度的差值，930为160
    private int tvSize = 35;//textview的字体大小，930为30
    private int tvpadding = 4;

    public MainRightPopupView(Context context, int width, int height, List<String> list) {
        super(width, height);
        mContext = context;
        mData = list;
        currentWidth = width;
//        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//        mView = mInflater.inflate(R.layout.popup_main_left,null);
        LinearLayout layout = new LinearLayout(context);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layout.setLayoutParams(params);
        layout.setGravity(Gravity.CENTER_HORIZONTAL);
        layout.setOrientation(LinearLayout.HORIZONTAL);
//        layout.setBackgroundColor(mContext.getResources().getColor(R.color.bg_line1));
        layout.setBackground(mContext.getResources().getDrawable(R.mipmap.bg_main_right));
        initValue();
        initText(layout, width);

        //设置view
        this.setContentView(layout);
        //设置宽度
        this.setWidth(width);
        //设置高度
        this.setHeight(height);
        //设置弹出框可点击
        this.setFocusable(true);
        //设置弹出框可触摸
        this.setTouchable(true);
        //实例化一个弹窗背景
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置弹窗背景
        this.setBackgroundDrawable(dw);


        layout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        currentWidth = layout.getMeasuredWidth();
        currentHeight = layout.getMeasuredHeight();
    }

    private void initValue() {
        if (Contents.SPORT_TYPE.equals(Type.PM810)) {
        } else if (Contents.SPORT_TYPE.equals(Type.PM820)) {
        } else if (Contents.SPORT_TYPE.equals(Type.PM930)) {
            tvW = 340;//
            tvSize = 35;
            tvpadding = 8;
        } else {//940
            if (Contents.isTTYS3) {
                tvW = 200;//
                tvSize = 25;
            } else {
                tvW = 200;//
                tvSize = 50;
            }

        }
    }

    private void initText(LinearLayout layout, int width) {
        int tvWidth = (width - tvW) / mData.size();
        Log.e("MainLeftPopupView", "tv的宽度  " + tvWidth);
        for (int i = 0; i < mData.size(); i++) {
            TextView tv = new TextView(mContext);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(tvWidth, tvWidth);
            params.setMargins(tvpadding, tvpadding, tvpadding, tvpadding);
            tv.setLayoutParams(params);
            tv.setBackgroundResource(R.drawable.bg_main_num);
            tv.setText(mData.get(i) + "");
            tv.setTextColor(Color.WHITE);
            tv.setTextSize(tvSize);
            tv.setGravity(Gravity.CENTER);
            tv.setOnClickListener(this);
            layout.addView(tv);
        }
    }

    @Override
    public void onClick(View view) {
        TextView tv = (TextView) view;
        MainActivity.instance.setPopupSpeed(tv.getText().toString());
        Log.e("MainLeftPopupView", "点击的值为  " + tv.getText().toString());
    }

    public int getCurrentWidth() {
        return currentWidth;
    }

    public int getCurrentHeight() {
        return currentHeight;
    }


}
